package com.android.aiod.ui.activity;

import android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.aiod.R;
import com.android.aiod.adapter.VoiveListAdapter;
import com.android.aiod.manager.VoiceManager;
import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.tool.Log;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.blankj.utilcode.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class VoiceActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.et_text)
    Comm_EditView et_text;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    VoiveListAdapter adapter;
    VoiceManager voiceManager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_voice;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("文字转语音");
        comm_title.setLeftClickFinish((Activity) mContext);

        et_text.setText("红豆生南国，春来发几枝。愿君多采撷，此物最相思。");

        adapter = new VoiveListAdapter(mContext, callBack);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(adapter);
    }

    CommCallBack callBack = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            TextToSpeech.EngineInfo engineInfo = (TextToSpeech.EngineInfo) obj;
            voiceManager.switchEngine(engineInfo.name);
            ToastUtils.showShort("已切换换语音包");
        }
    };


    @Override
    protected void initData() {
        voiceManager = new VoiceManager(mContext);
        adapter.setData(voiceManager.getTTSList());

        for (TextToSpeech.EngineInfo engineInfo : voiceManager.getTTSList()) {
            Log.i("engineInfo:" + engineInfo.name);
            if ("com.xiaomi.mibrain.speech".equals(engineInfo.name)) {
                voiceManager.switchEngine(engineInfo.name);
            }
        }
    }

    @OnClick({R.id.tv_anim})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_anim:  //动画
                String text = et_text.getText();
                if (TextUtils.isEmpty(text)) {
                    ToastUtils.showShort("文字不能为空");
                    return;
                }
                voiceManager.play(text);
                break;
        }
    }
}
